$(function () {
    $("#jqGrid").Grid({
        url: '../collage/list',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '商品ID', name: 'goodsId', index: 'goods_id', width: 50},
			{label: '拼单量', name: 'collageNum', index: 'collage_num', width: 50},
			{label: '拼单价格', name: 'collagePrice', index: 'collage_price', width: 60},
			{label: '是否有客户等级优惠', name: 'isClientDiscount', index: 'is_client_discount', width: 100, formatter: function (value) {
                    if (value == '0')
                        return '否';
                    else if (value == '1')
                        return '是';
                    else
                        return '-';
                }},
			{label: '客户等级优惠', name: 'clientDiscount', index: 'client_discount', width: 70, formatter: function (value) {
                    if (value != null)
                        return value;
                    else
                        return '-';
                }},
			{label: '是否有高级客户等级优惠', name: 'isVipClientDiscount', index: 'is_vip_client_discount', width: 110, formatter: function (value) {
					if (value == '0')
						return '否';
					else if (value == '1')
						return '是';
					else
						return '-';
                }},
			{label: '高级客户等级优惠', name: 'vipClientDiscount', index: 'vip_client_discount', width: 90, formatter: function (value) {
					if (value != null)
						return value;
					else
						return '-';
                }},
			{
				label: '是否启用', name: 'isActive', index: 'is_active', width: 50, formatter: function (value) {
					if (value == '0')
						return '禁用';
					else if (value == '1')
						return '启用';
					else
						return '-';
                }
			},
			{label: '截止日期', name: 'endDate', index: 'end_date', width: 50},
			{label: '当前销售量', name: 'currNum', index: 'curr_num', width: 60},
            {
                label: '操作', width: 60, align: 'center', sortable: false, formatter: function (value, col, row) {
                    return '<button class="btn btn-outline btn-info" onclick="vm.sendGoods(' + row.id + ')"><i class="fa fa-info-circle"></i>&nbsp;发货</button>';
                }
            }
		]
    });
});

var vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
		collage: {
            isClientDiscount: 0,
            vipClientDiscount: 0,
            isActive: 0,
		},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			],
		},
		q: {
		    name: '',
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.collage = {};
		},
		update: function (event) {
            var id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            var url = vm.collage.id == null ? "../collage/save" : "../collage/update";
            Ajax.request({
				type: "POST",
			    url: url,
				contentType: "application/json",
                params: JSON.stringify(vm.collage),
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            var ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../collage/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../collage/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.collage = r.collage;
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
                name: ''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
		//未完成
        sendGoods: function (event) {

        },
	}
});